﻿using UnityEngine;

namespace JH
{
    /// <summary>
    ///  横向分割线Mono
    /// </summary>
    public class EachHorizontalDividingLine : BasicDivisionLine
    {
        /// <summary>
        ///  设置分割线的位置，平行于X轴的分割线
        /// </summary>
        /// <param name="newValue">新的分割线对应的值</param>
        protected override void OnSetValue(float newValue)
        {
            //设置分割线位置
            SetPosition(newValue);
        }

        /// <summary>
        ///  设置分割线位置
        /// </summary>
        private void SetPosition(float newValue)
        {
            // 获取当前X轴线条的RectTransform组件
            var rect = GetComponent<RectTransform>();

            //构造一个数据点，求取其UV坐标
            var uv = BoundaryHandler.GetUVByOriginPoint(new Vector2(0, newValue));

            // 根据UV坐标以及图表的高度，计算X轴线条的位置
            var height = uv.y * BoundaryHandler.Height;

            //直接设置给线条的锚点位置
            rect.anchoredPosition = new Vector2(0, height - rect.sizeDelta.y / 2);
        }

        protected override void OnValidate()
        {
            base.OnValidate(); 
            var rect = GetComponent<RectTransform>();
            if (rect)
            {
                rect.anchorMin = new Vector2(0, 0);
                rect.anchorMax = new Vector2(1, 0);
                rect.pivot = new Vector2(0.5f, 0);
                rect.sizeDelta = new Vector2(0, rect.sizeDelta.y);
            }
        }
    }
}